using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using WinformTemplete.Models;
using WinformTemplete.Modules.Blog.Models;
using WinformTemplete.Modules.Blog.Repositories;
using NLog;

namespace WinformTemplete.Modules.Blog.Services
{
    /// <summary>
    /// 博客服务实现
    /// </summary>
    public class BlogService : IBlogService
    {
        private readonly IBlogRepository _blogRepository;
        private readonly ILogger _logger;

        public BlogService(IBlogRepository blogRepository)
        {
            _blogRepository = blogRepository;
            _logger = LogManager.GetCurrentClassLogger();
        }

        public async Task<List<BlogModel>> GetAllBlogsAsync()
        {
            try
            {
                return await _blogRepository.GetAllAsync();
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "获取所有博客失败");
                throw;
            }
        }

        public async Task<BlogModel> GetBlogByIdAsync(int id)
        {
            try
            {
                return await _blogRepository.GetByIdAsync(id);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"获取博客失败，ID: {id}");
                throw;
            }
        }

        public async Task<bool> AddBlogAsync(BlogModel blog)
        {
            try
            {
                if (blog == null)
                    throw new ArgumentNullException(nameof(blog));

                blog.CreatedDate = DateTime.Now;
                return await _blogRepository.AddAsync(blog);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "添加博客失败");
                throw;
            }
        }

        public async Task<bool> UpdateBlogAsync(BlogModel blog)
        {
            try
            {
                if (blog == null)
                    throw new ArgumentNullException(nameof(blog));

                return await _blogRepository.UpdateAsync(blog);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"更新博客失败，ID: {blog.Id}");
                throw;
            }
        }

        public async Task<bool> DeleteBlogAsync(int id)
        {
            try
            {
                return await _blogRepository.DeleteAsync(id);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"删除博客失败，ID: {id}");
                throw;
            }
        }

        public async Task<List<BlogModel>> SearchBlogsAsync(string keyword)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(keyword))
                    return await GetAllBlogsAsync();

                return await _blogRepository.SearchByTitleAsync(keyword);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"搜索博客失败，关键词: {keyword}");
                throw;
            }
        }

        public async Task<List<BlogModel>> GetRecentBlogsAsync(int count)
        {
            try
            {
                return await _blogRepository.GetRecentBlogsAsync(count);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"获取最近博客失败，数量: {count}");
                throw;
            }
        }

        public async Task<int> GetTotalBlogCountAsync()
        {
            try
            {
                return await _blogRepository.GetTotalCountAsync();
            }
            catch (Exception ex)
            {
                _logger.Error(ex, "获取博客总数失败");
                throw;
            }
        }

        public async Task<List<BlogModel>> GetPagedBlogsAsync(int pageIndex, int pageSize)
        {
            try
            {
                return await _blogRepository.GetPagedBlogsAsync(pageIndex, pageSize);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"分页获取博客失败，页码: {pageIndex}, 每页数量: {pageSize}");
                throw;
            }
        }

        public async Task<List<BlogModel>> GetBlogsByDateRangeAsync(DateTime startDate, DateTime endDate)
        {
            try
            {
                return await _blogRepository.GetBlogsByDateRangeAsync(startDate, endDate);
            }
            catch (Exception ex)
            {
                _logger.Error(ex, $"根据日期范围获取博客失败，开始日期: {startDate}, 结束日期: {endDate}");
                throw;
            }
        }
    }
} 